<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('Login_model');
		$this->load->library('Yzm');
		$this->load->library('Js');
	}
	public function login()
	{
		if(isset($_POST['name']))
		{
			$data=$_POST;
			if(!Yzm::returnCode($data['yzm'])){ajax_return(0,'验证码错误');}
			if(trim($data['name'])==''){ajax_return(0,'账号不能为空');}
			$password=trim($data['password']);
			if(strlen($password)<6){ajax_return(0,'密码长度不能小于6位');}
			if(!check_regex($data['password'],'isnozh') ){ajax_return(0,'密码不能有中文字符');}
			//查询用户是否存在
			$userlist=$this->Login_model->pm_user_list_name($data['name']);
			if(!$userlist){ajax_return(0,'账号不存在');}
			if($userlist['status']==0){ajax_return(0,'请联系管理员激活账号');}
			if($userlist['status']==2){ajax_return(0,'请联系管理员重新激活账号');}
			$mdpassword=userpassmd5($password);
			if($userlist['pwd']!=$mdpassword){ajax_return(0,'密码错误');}
			//更改登陆记录
			$usertoken=getuniqidstring($userlist['id']);
			$userlist['usertoken']=$usertoken;
			unset($userlist['pwd']);
			$_SESSION['pmsuserinfo']=serialize($userlist);
			if(!$this->Login_model->pm_user_login_editinfo($userlist['id'],$usertoken)){}
			$remark=$userlist['name']."登陆成功";
			useraddrecord('用户登陆',$remark);
			ajax_return(1,'登陆成功');
		}
		else
		{
			$this->display('login/login.html');
		}
	}
	public function verity()
	{
		$setconfig=[
			'width'=>120,//宽带
			'height'=>42,//高度
			'strlength'=>5,//字符长度
			'bgcolor'=>'#B0E0E6',//背景颜色 随机
			'pixelcolor'=>'#FFFFFF',//像素点颜色
			'textcolor'=>false,//字体颜色
			'linecolor'=>'#FFFFFF',//线条颜色
		];
		Yzm::setconfig($setconfig);
		return Yzm::run();
	}
	public function loginout(){
		if(isset($_SESSION['pmsuserinfo'])){
			//更新用户下线状态
			$userlist=unserialize($_SESSION['pmsuserinfo']);
			$remark=$userlist['name']."退出";
			useraddrecord('用户登出',$remark);
			unset($_SESSION['pmsuserinfo']);
		}
		echo Js::top_to_url("/Login/login");
	}
}
